<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\cpmjagent\controller\finance;

/**
 * Description of SettlementController
 *
 * @author SoChishun
 */
class SettlementController extends \app\cpmjagent\controller\AppbaseController {

    public function withdraw() {
        $uid = $this->global_data['userid'];
        $banklist = \app\cpmjagent\model\finance\BankcardModel::all(['user_id' => $uid]);
        if (!$banklist) {
            $this->error('您未填写提现银行卡信息!', url('bankcardinfo'));
        }
        $blance = \app\cpmjagent\model\user\AgentModel::where(['id' => $uid])->value('blance');
        return view('', ['blance' => $blance, 'banklist' => $banklist]);
    }

    public function doWithdraw($amount = 0, $bankcardid = 0) {
        $uid = $this->global_data['userid'];
        $bankcarddata = \app\cpmjagent\model\finance\BankcardModel::get($bankcardid);
        $data = [
        'out_trade_no' => 'TX'.date('YmdHis'),
        'amount' => $amount,
        'bankname' => $bankcarddata['bankname'],
        'bankcardholder' => $bankcarddata['bankcardholder'],
        'bankcardno' => $bankcarddata['bankcardno'],
        'user_id' => $uid,
        'remark' => '',
        'status' => 0,
        ];
        \app\cpmjagent\model\finance\WithdrawModel::create($data);
        $where=['id'=>$uid];
        \app\cpmjagent\model\user\AgentModel::where($where)->setDec('blance',$amount);
        \app\cpmjagent\model\user\AgentModel::where($where)->setInc('blance_frozen',$amount);
        $this->success('提现申请成功', url('withdrawlist'));
    }

    public function settlementlist() {
        $bankcard = \app\cpmjagent\model\finance\BankcardModel::get(['user_id' => $this->global_data['userid']]);
        if (!$bankcard) {
            $this->redirect('accountinfo');
        }
        $list = \app\cpmjagent\model\finance\SettlementModel::where(function($query) {
                    $query->order('id desc');
                })->paginate(10);
        return view('', ['list' => $list]);
    }

    public function bankcardinfo() {
        $uid = $this->global_data['userid'];
        $bankcarddata = \app\cpmjagent\model\finance\BankcardModel::get(['user_id' => $uid, 'account_type' => 1]);
        $alipaydata = \app\cpmjagent\model\finance\BankcardModel::get(['user_id' => $uid, 'account_type' => 2]);
        return view('', ['alipaydata' => $alipaydata, 'bankcarddata' => $bankcarddata]);
    }

    public function saveBankcardInfo($zhifubao = '', $bankname = '', $bankcardholder = '', $bankcardno = '', $alipayid = 0, $bankcardid = 0) {
        if ($zhifubao && !xsfn_validate_string($zhifubao, 6, 32)) {
            $this->error('支付宝无效', url('bankcardinfo'), '', 2);
        }
        if (!xsfn_validate_string($bankname, 6, 32)) {
            $this->error('开户行无效', url('bankcardinfo'), '', 2);
        }
        if (!xsfn_validate_string($bankcardholder, 2, 32)) {
            $this->error('持卡人无效', url('bankcardinfo'), '', 2);
        }
        if (!xsfn_validate_string($bankcardno, 6, 32)) {
            $this->error('卡号无效', url('bankcardinfo'), '', 2);
        }
        $uid = $this->global_data['userid'];
        $mBankcard = new \app\cpmjagent\model\finance\BankcardModel();
        if ($zhifubao) {
            $alipaydata['bankcardno'] = $zhifubao;
            $alipaydata['account_type'] = 2; // 1:银行卡;2:支付宝;3:微信
            $alipaydata['user_id'] = $uid;
            $alipaydata['title'] = '支付宝:' . substr($zhifubao, 0, 3) . '...' . substr($zhifubao, strlen($zhifubao) - 3);
            if ($alipayid) {
                $mBankcard->save($alipaydata, ['id' => $alipayid]);
            } else {
                $mBankcard->save($alipaydata);
            }
        }
        $data['bankname'] = $bankname;
        $data['bankcardholder'] = $bankcardholder;
        $data['bankcardno'] = $bankcardno;
        $data['account_type'] = 1;
        $data['user_id'] = $uid;
        $data['title'] = substr($bankcardno, 0, 3) . '...' . substr($bankcardno, strlen($bankcardno) - 3);
        if ($bankcardid) {
            $mBankcard->save($data, ['id' => $bankcardid]);
        } else {
            $mBankcard->save($data);
        }
        $this->success('保存成功', url('withdraw'));
    }

    public function doRechargeSubmit() {
        $data = [
            'account' => 'tuzi',
            'out_trade_no' => date('YmdHis'),
            'amount' => 1,
            'pay_channel' => 'wxjsapi',
            'user_id' => 1,
            'remark' => 'test',
            'status' => 0,
        ];
        \app\cpmjagent\model\finance\RechargeModel::create($data);
        //$sqlstmt = 'insert into t_finance_recharge (account, out_trade_no, amount, pay_channel, user_id, remark, status) values (:account, :out_trade_no, :amount, :pay_channel, :user_id, :remark, 1)';
        return 'ok';
    }

}
